const StudentService = require("../service/StudentService")
const JWT = require("../util/JWT")
const StudentController = {
    getList: async (req, res) => {
        const result = await StudentService.getList()
        res.send(result)
    },
    addList:async (req,res) => {
        // console.log(req.body);
        const result = await StudentService.addList(req.body)
        res.send(result)
    },
    login:async (req,res)=>{
        const result = await StudentService.login(req.body)
        if(result.length){
            let {_id,studentname,introduction,avatarUrl,gender} = result[0]
            const token = JWT.generate({
                _id,
                username:studentname
            },"7d")
            //将token返回前端的响应头
            res.header("Authorization",token)
            res.send({
                message:"OK",
                // data:
                data:{_id,username:studentname,
                    introduction,avatarUrl,gender,
                    role:{
                        "roleName": "学生",
                        "roleType": 3,
                        "rights": [
                            "/index",
                            "/center",
                            "/interview-manage",
                            "/interview-manage/companylist",
                            "/interview-manage/companydata",
                            "/student-manage",
                            "/student-manage/studentlist",
                        ]
                    }
                }
            })
        }else{
            res.status(400).send({
                message:'用户名或密码错误'
            })
        }
    },
    deleteList:async (req,res) => {
        const result = await StudentService.deleteList(req.params.id)
        res.send(result)
    },
    updateList:async(req,res) => {
        const result = await StudentService.updateList(req.params.id,req.body)
        res.send(result)
    },
    upload:async(req,res)=>{
        console.log('txt',req.file,req.body);
        const result = await StudentService.upload(req.params.id,req.file,req.body)
        res.send(result)
    }
}
module.exports = StudentController